package com.healthy.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Maple.Chen
 * @date 2020年1月1日
 * @description <p>修改工程的配置文件</p>
 * @Range "/Project/WEB-INF/classes/*.properties"
 */
public class PropertiesUtil {
//	web调用方式
//	String path=this.getClass().getClassLoader().getResource("my.properties").getPath(); //WEB-INF的Classes，热部署，改动后会auto重启tomcat
//	String path2= this.getServletContext().getRealPath("/WEB-INF/my.properties");  //WEB-INF下，tomcat不会因其改变而重启
//	File f=new File(path); // 文件参数路径
	// 如果出现文字后有乱码或\t     用String的replace("\t","")方法
	/*当前Controller采用path2路径*/
	
	//添加(/更新)键值对<字符流纯文本>[不可插入中文:会乱码]  (若Key存在则更新)
	public static boolean Add(String Key,String Value,File file) throws IOException{
		Map<String,String> map=new HashMap<>();
		//read
		FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
        String line="";
        String[] arrs=null;
        while ((line=br.readLine())!=null) {
        	if(line.equals("")||line.startsWith("#")) continue;
            arrs=line.split("=");
            map.put(arrs[0],arrs[1]);
        }
        br.close();
        fr.close();
        //decide
        if(map.containsKey(Key)){
        	map.replace(Key,Value);
        }else{
        	map.put(Key,Value);
        }
		//write
        FileWriter fw=new FileWriter(file);
        BufferedWriter  bw=new BufferedWriter(fw);
        Iterator<Map.Entry<String,String>> it=map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String,String> m=it.next();
			bw.write(m.getKey()+"="+m.getValue()+"\n");
		}
        bw.close();
        fw.close();
		return true;
	}
	//删除键值对
	public static boolean Del(String Key,File file) throws IOException{
		Map<String,String> map=new HashMap<>();
		//read
		FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
        String line="";
        String[] arrs=null;
        while ((line=br.readLine())!=null) {
        	if(line.equals("")||line.startsWith("#")) continue;
            arrs=line.split("=");
            if(arrs[0].equals(Key)){ }else{ map.put(arrs[0],arrs[1]); }
        }
        br.close();
        fr.close();
		//write
        FileWriter fw=new FileWriter(file);
        BufferedWriter  bw=new BufferedWriter(fw);
        Iterator<Map.Entry<String,String>> it=map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String,String> m=it.next();
			bw.write(m.getKey()+"="+m.getValue()+"\n");
		}
        bw.close();
        fw.close();//(先开后关)
		return true;
	}
	//查找键值对<字符流纯文本>[检索中文会乱码]
	public static String Find(String Key,File file) throws IOException{
		String result="null";
		FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
//		FileInputStream fileInputStream=new FileInputStream(file);
//	    InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream, "UTF-8");
//	    BufferedReader br=new BufferedReader(inputStreamReader);
        String line="";
        String[] arrs=null;
        while ((line=br.readLine())!=null) {
        	if(line.equals("")||line.startsWith("#")) continue;
            arrs=line.split("=");
            if(Key.equals(arrs[0])){
            	result=arrs[1];
            	break;
            }
        }
        br.close();
        fr.close();
//        fr.close();//(先开后关)
//        br.close();
//        inputStreamReader.close();
//        fileInputStream.close();
		return result;
	}
	//输出所有键值对<字符流纯文本*不含中文>[检索中文会乱码]
	public static Map<String,String> FindAll(File file) throws IOException{
		Map<String,String> map=new HashMap<>();
		FileReader fr=new FileReader(file);
		BufferedReader br=new BufferedReader(fr);
		String line="";
		String[] arrs=null;
		while ((line=br.readLine())!=null) {
			if(line.equals("")||line.startsWith("#")) continue;
			arrs=line.split("=");
//			System.out.println("arr="+Arrays.toString(arrs));
			map.put(arrs[0],arrs[1]);
		}
		br.close();
		fr.close();//(先开后关)
		return map;
	}
	
}
